//
//  MessageAddressee.m
//  MailDelivery
//
//  Created by Dante Palacios on 9/29/09.
//  Copyright 2009 Dante Palacios. All rights reserved.
//

#import "MessageAddressee.h"

#import "NSStringAdditions.h"

#import "Funtions.h"
#import "Macros.h"


@implementation MessageAddressee

@synthesize address = _address;
@synthesize displayName = _displayName;
@synthesize userName = _userName;
@synthesize domain = _domain;
@synthesize hostName = _hostName;
@synthesize representedObject = _representedObject;


- (id)initWithDisplayName:(NSString *)name address:(NSString *)emailAddress
{
	self = [super init];
	if (self != nil)
	{
		if (!emailAddress.length) {
			[self release], self = nil;
		}
		else 
		{
			self.address = emailAddress;
			self.displayName = name ? name : @"";	
		}
	}
	return self;
}

- (id)initWithString:(NSString *)string 
{
	return [self initWithDisplayName:[string displayName] address:[string address]];
}

- (id)copyWithZone:(NSZone *)zone 
{
	MessageAddressee *emailAddress = [[[self class] allocWithZone:zone] init];
	emailAddress.displayName = self.displayName;
	emailAddress.address = self.address;
	emailAddress.domain = self.domain;
	emailAddress.hostName = self.hostName;
	emailAddress.representedObject = self.representedObject;
	return emailAddress;
}

- (void)encodeWithCoder:(NSCoder *)coder 
{
	[coder encodeObject:self.address forKey:@"address"];
	[coder encodeObject:self.displayName forKey:@"name"];
    [coder encodeObject:self.userName forKey:@"userName"];
    [coder encodeObject:self.domain forKey:@"domain"];
	[coder encodeObject:self.hostName forKey:@"hostName"];
	
	if ([self.representedObject conformsToProtocol:@protocol(NSCoding)]) 
	{
		[coder encodeObject:self.representedObject forKey:@"representedObject"];
	}
}

- (id)initWithCoder:(NSCoder *)decoder 
{
	MessageAddressee *internetAddress = [[[self class] alloc] init];
	internetAddress.address = [decoder decodeObjectForKey:@"address"];
	internetAddress.displayName = [decoder decodeObjectForKey:@"name"];
	internetAddress.userName = [decoder decodeObjectForKey:@"userName"];
	internetAddress.domain = [decoder decodeObjectForKey:@"domain"];
	internetAddress.hostName = [decoder decodeObjectForKey:@"hostName"];
	internetAddress.representedObject = [decoder decodeObjectForKey:@"representedObject"];
    return internetAddress;
}

- (void)dealloc 
{
	[_address release];
	_address = nil;
	[_displayName release];
	_displayName = nil;
	[_userName release];
	_userName = nil;
	[_domain release];
	_domain = nil;
	[_hostName release];
	_hostName = nil;
	[_fullEmailAddress release];
	_fullEmailAddress = nil;
	[_representedObject release];
	_representedObject = nil;
	
	[super dealloc];
}

- (BOOL)isValid; 
{
	return IsValidEmail(self.address);
}

#pragma mark getters & setters

- (NSString *)address 
{
	return AtomicAutoreleasedGet(_address);
}

- (void)setAddress:(NSString *)address 
{
	NSParameterAssert(address);
	AtomicCopiedSet(_address, address);
	
	[_fullEmailAddress release];
	_fullEmailAddress = nil;
}

- (NSString *)displayName 
{
    return AtomicAutoreleasedGet(_displayName);
}

- (void)setDisplayName:(NSString *)value 
{
	AtomicCopiedSet(_displayName, value);
	
	[_fullEmailAddress release], _fullEmailAddress = nil;
}

- (NSString *)userName 
{
	NSString *value = nil;
	@synchronized(self) 
	{
		if (!_userName) 
		{
			_userName = [[self.address userName] retain];
		}
		value = [[_userName retain] autorelease];
	}
	
    return value;
}

- (void)setUserName:(NSString *)value 
{
	AtomicCopiedSet(_userName, value);
}

- (NSString *)domain 
{
	NSString *value = nil;
	@synchronized(self)
	{
		if (!_domain) 
		{
			_domain = [[self.address domain] retain];
		}
		value = [[_domain retain] autorelease];
	}
	
    return value;
}

- (void)setDomain:(NSString *)value
{
	AtomicCopiedSet(_domain, value);
}

- (NSString *)hostName
{
	NSString *value = nil;
	@synchronized(self)
	{
		if (!_hostName)
		{
			_hostName = [[self.address hostName] retain];
		}
		value = [[_hostName retain] autorelease];
	}
	
    return value;
}

- (void)setHostName:(NSString *)value
{
	AtomicCopiedSet(_hostName, value);
}

- (id)representedObject
{
    return AtomicAutoreleasedGet(_representedObject);
}

- (void)setRepresentedObject:(id)value
{
    AtomicRetainedSet(_representedObject, value);
}

- (NSString *)fullEmailAddress
{
	NSString *value = nil;
	@synchronized(self)
	{
		if (!_fullEmailAddress)
		{
			if (!self.displayName) self.displayName = @"";
			
			_fullEmailAddress = [[NSString stringWithFormat:!self.displayName.length ? @"%@%@" : @"%@ <%@>", self.displayName, self.address] retain];
		}
		
		value = [[_fullEmailAddress retain] autorelease];
		
	}
	return value;
}

- (NSString *)description; 
{
	return [self fullEmailAddress];
}

@end
